
***********************
***********************
**Regresiones básicas**
***********************
***********************
clear
use "$base_out/alum_pb_2018_modified"
**********AGREGANDO VARIABLES DE ESTRATIFICACION*****

***PEC PLUS VS. PEC****
label var PEC_Plus " "
label var PEC " "

eststo clear

foreach x in mat_std leg_std promedio_std prom_pca {

	eststo: reghdfe `x'  PEC_Plus  if (treatgroup2!="Control" & state!=10) , cluster(cct_num) a(estrato)
	matrix tempm=e(b)
	local tempm=string(tempm[1,1], "%9.2fc")
	local tempm_effect=tempm[1,1]

	file open newfile using "$tables/`x'_coef_PEC_PLUS_vs_PEC.tex", write replace
	file write newfile "`tempm'"
	file close newfile
	test PEC_Plus
	if r(p)<0.001 {
		di "peque"
		local tempm ="$<0.001$"
		file open newfile using "$tables/`x'_pvalue_PEC_PLUS_vs_PEC.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	if r(p)>0.001 {
		di "grande"
		local tempm=string(r(p), "%9.2fc")
		file open newfile using "$tables/`x'_pvalue_PEC_PLUS_vs_PEC.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	*matrix b_`x'=e(b)
	*estimates store m1`x'
	
	lincom PEC_Plus
	matrix tempm=r(estimate)+invttail(r(df),0.025)*r(se)
	local tempm=string(tempm[1,1], "%9.2fc")

	file open newfile using "$tables/`x'_RuleOut_PEC_PLUS_vs_PEC.tex", write replace
	file write newfile "`tempm'"
	file close newfile
	
}
*matrix betas1= (b_mat_std[1,1]\b_leg_std [1,1]\b_promedio_std [1,1]\b_prom_pca[1,1])

esttab using "$tables/Resultados_PEC_PLUSVSPECESTR.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	keep(PEC_Plus) stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
	
	
	***Para Estados****
	
foreach sta in 1 3 4 5 6 7 9{
	eststo clear
eststo:reghdfe mat_std  PEC_Plus  if (treatgroup2!="Control" & state==`sta') , cluster(cct_num) a(estrato)

matrix tempm=e(b)
local tempm=string(tempm[1,1], "%9.2fc")
local tempm_effect=tempm[1,1]

file open newfile using "$tables/mat_std_coef_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
file write newfile "`tempm'"
file close newfile
test PEC_Plus
if r(p)<0.001 {
	di "peque"
	local tempm ="$<0.001$"
	file open newfile using "$tables/mat_std_pvalue_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
if r(p)>0.001 {
	di "grande"
	local tempm=string(r(p), "%9.2fc")
	file open newfile using "$tables/mat_std_pvalue_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
lincom PEC_Plus
matrix tempm=r(estimate)+invttail(r(df),0.025)*r(se)
local tempm=string(tempm[1,1], "%9.2fc")

file open newfile using "$tables/mat_std_RuleOut_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
file write newfile "`tempm'"
file close newfile



foreach x in leg_std promedio_std prom_pca {

	eststo: reghdfe `x'  PEC_Plus  if (treatgroup2!="Control" & state==`sta') , cluster(cct_num) a(estrato)
	matrix tempm=e(b)
	local tempm=string(tempm[1,1], "%9.2fc")
	local tempm_effect=tempm[1,1]

	file open newfile using "$tables/`x'_coef_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
	file write newfile "`tempm'"
	file close newfile
	test PEC_Plus
	if r(p)<0.001 {
		di "peque"
		local tempm ="$<0.001$"
		file open newfile using "$tables/`x'_pvalue_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	if r(p)>0.001 {
		di "grande"
		local tempm=string(r(p), "%9.2fc")
		file open newfile using "$tables/`x'_pvalue_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	lincom PEC_Plus
	matrix tempm=r(estimate)+invttail(r(df),0.025)*r(se)
	local tempm=string(tempm[1,1], "%9.2fc")

	file open newfile using "$tables/`x'_RuleOut_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
	file write newfile "`tempm'"
	file close newfile
	
}

esttab using "$tables/Resultados_PEC_PLUSVSPECESTR_state_`sta'.tex" ,  fragment se ar2 nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	keep(PEC_Plus) coef(none) stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
	
}	
	

	
	

	
	

